import subprocess
import re

def check_pcie_width():
    try:
        # 执行lspci命令并获取输出
        output = subprocess.check_output(
            ["lspci", "-vvd", "10de:"], 
            stderr=subprocess.STDOUT,
            universal_newlines=True
        )
        
        # 查找所有LnkSta行
        matches = re.finditer(r"LnkSta:\s+Speed\s+\d+GT/s\s+\((ok|downgraded)\)\,\s+Width\s+x(\d+)\s+\((ok|downgraded)\)", output)
        
        for match in matches:
            width = match.group(2)
            status = match.group(3)
            if width == "16" and status == "ok":
                return 0
        
        return 1
    
    except subprocess.CalledProcessError as e:
        print(f"命令执行失败: {e.output}")
        return 1
    except Exception as e:
        print(f"发生错误: {str(e)}")
        return 1

if __name__ == "__main__":
    exit_code = check_pcie_width()
    print(exit_code)